//
//  OnLIneTableViewController.swift
//  豆瓣电台.Swift
//
//  Created by rongfeng on 16/3/1.
//  Copyright © 2016年 rongfeng. All rights reserved.
//

import UIKit

class OnLIneTableViewController: UITableViewController {
   //?
    var channelList:Array<Channel> = []
    var Curchannel :Channel!
    
    override init(style: UITableViewStyle) {
        super.init(style: style);
    }
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder);
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        //获取数据
        
        loadChannelList();
        
        
    }
   //获取数据
    func loadChannelList() {
        
        let url = NSURL(string: "https://gitcafe.com/wcrane/XXXYYY/raw/master/baidu.json");
        let request = NSURLRequest(URL: url!);
        let mainQuene = NSOperationQueue.mainQueue();
        NSURLConnection.sendAsynchronousRequest(request, queue: mainQuene) { (response, Data, error) -> Void in
            let httpResponse = response as! NSHTTPURLResponse;
            if httpResponse.statusCode == 200 {
                let str = NSString(data: Data!, encoding: NSUTF8StringEncoding);
                print("网络歌曲：\(str)");
                //将JSON转化为字符串
               
                var array:NSArray =
               try! NSJSONSerialization.JSONObjectWithData(Data!, options: NSJSONReadingOptions.AllowFragments) as! NSArray;
                print("转化后的数组:\(array)");
                //self.channelList = [];
                //遍历到全局数组里面
                for dict:AnyObject in array {
                    
                    var channel = Channel(dict: dict as! NSDictionary)
                    self.channelList.append(channel);
                    
                }
                
                print("转化后的数据:\(self.channelList)");
                
                self.tableView.reloadData();
                
                
            }
            else {
                
            }
        }
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // MARK: - Table view data source

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
       
        return self.channelList.count;
        
    }
    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell:UITableViewCell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as UITableViewCell;
        let channelTitle:Channel = self.channelList[indexPath.row];
        cell.textLabel?.text = String(channelTitle.title);
        return cell;
    }
    
    override func tableView(tableView: UITableView, willSelectRowAtIndexPath indexPath: NSIndexPath) -> NSIndexPath? {
        Curchannel = self.channelList[indexPath.row];
        return indexPath;
    }
    
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        let destViewController: OnLineViewController = segue.destinationViewController as! OnLineViewController
        destViewController.channel = Curchannel;
    }
    
}
